/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pz.net.master;

import java.net.SocketAddress;

/**
 *
 * @author jannek
 */
public class Client implements Comparable<Client> {

    private static int currentId = 0;

    private static int getNextId() {
        return ++currentId;
    }

    public final String name;
    public final int id;
    public final int key;
    private SocketAddress address;
    private boolean active;

    public long nextUpdate;
    public boolean updateSent;

    public Client(String name) {
        id = getNextId();
        key = (int) (Math.random() * Integer.MAX_VALUE * 2 + Integer.MIN_VALUE);
        this.name = name;
        active = false;
    }

    public void setAddress(SocketAddress address) {
        if (address == null)
            return;
        this.address = address;
        nextUpdate = System.currentTimeMillis() + 120000;
        active = true;
    }

    public SocketAddress getAddress() {
        if (!active)
            return null;
        return address;
    }

    public boolean isKey(int key) {
        return this.key == key;
    }

    @Override
    public int compareTo(Client c) { // test later
        if (updateSent) {
            return c.nextUpdate > System.currentTimeMillis() ? -1 : 1;
        }
        return nextUpdate < c.nextUpdate ? -1 : 1;
    }
}
